//
//  HMHomeToolBar.swift
//  Weibo
//
//  Created by Apple on 2017/7/13.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit
import SnapKit
//  首页toolbar视图
class HMHomeToolBar: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        let retweetButton = addChildButton(title: "转发", imageName: "timeline_icon_retweet")
        let commentButton = addChildButton(title: "评论", imageName: "timeline_icon_comment")
        let unlikeButton = addChildButton(title: "赞", imageName: "timeline_icon_unlike")
        
        retweetButton.snp.makeConstraints { (make) in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(commentButton)
        }
        commentButton.snp.makeConstraints { (make) in
            make.leading.equalTo(retweetButton.snp.trailing)
            make.top.bottom.equalTo(self)
            make.width.equalTo(unlikeButton)
        }
        unlikeButton.snp.makeConstraints { (make) in
            make.top.trailing.bottom.equalTo(self)
            make.leading.equalTo(commentButton.snp.trailing)
        }
        
        let firstLineView = addLineView()
        let secondLineView = addLineView()
        firstLineView.snp.makeConstraints { (make) in
            make.centerX.equalTo(retweetButton.snp.trailing)
            make.centerY.equalTo(retweetButton)
        }
        secondLineView.snp.makeConstraints { (make) in
            make.centerX.equalTo(commentButton.snp.trailing)
            make.centerY.equalTo(commentButton)
        }
    }
    
    //  添加子按钮
    private func addChildButton(title: String, imageName: String) -> UIButton {
        let button = UIButton()
        button.setTitle(title, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setImage(UIImage(named: imageName), for: .normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), for: .normal)
        button.adjustsImageWhenHighlighted = false
        addSubview(button)
        return button
    }
    
    //  添加子线
    private func addLineView() -> UIImageView {
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(imageView)
        return imageView
    }

}
